﻿using System.Diagnostics.CodeAnalysis;

namespace ZeroSum.Domain.Entities;

public abstract class BaseEntity : IEntity
{
    public abstract object[] GetKeys();
}

/// <summary>
///     实体接口
/// </summary>
public interface IEntity
{
}

public abstract class BaseEntity<TKey> : BaseEntity
{
    [AllowNull] public virtual TKey Id { get; set; }

    public override object[] GetKeys()
    {
        return new object[] {Id!};
    }

    public override bool Equals(object? obj)
    {
        if (obj == null) return false;
        return obj is BaseEntity<TKey> entity && IsKeyEqual(entity.Id, Id);
    }

    public static bool IsKeyEqual(TKey id1, TKey id2)
    {
        if (id1 == null && id2 == null) return true;
        if (id1 == null || id2 == null) return false;
        return Equals(id1, id2);
    }

    public override int GetHashCode()
    {
        return Id == null ? 0 : Id.ToString()!.GetHashCode();
    }
}